package com.iamgod.savemoney.base;

import com.iamgod.savemoney.R;
import com.iamgod.savemoney.goods.SearchGoodsAct;
import com.iamgod.savemoney.plan.CMStatementsAct;
import com.iamgod.savemoney.plan.MStatementsAct;
import com.iamgod.savemoney.plan.PlanListAct;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class BaseActivity extends Activity {
	
//	public void createSysDataBase(){
//		SQLiteDatabase sdb = this.openOrCreateDatabase(Config.DataBase.DATABASE_NAME, MODE_PRIVATE, null);
//		sdb.execSQL(Config.DataBase.Create_Table_PlanConfig);
//	}
	
	public void initNav(final Activity act){
		
		TextView tv_nav_1,tv_nav_2,tv_nav_3,tv_nav_4;
		
		tv_nav_1 = (TextView) findViewById(R.id.tv_nav_1);
		tv_nav_2 = (TextView) findViewById(R.id.tv_nav_2);
		tv_nav_3 = (TextView) findViewById(R.id.tv_nav_3);
		tv_nav_4 = (TextView) findViewById(R.id.tv_nav_4);
		
		tv_nav_1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(act, CMStatementsAct.class);
				startActivity(i);
			}
			
		});
		tv_nav_2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(act, PlanListAct.class);
				startActivity(i);
			}
			
		});
		tv_nav_3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(act, MStatementsAct.class);
				startActivity(i);
			}
			
		});
		tv_nav_4.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(act, SearchGoodsAct.class);
				startActivity(i);
			}
			
		});
	}
	
	/**
	 * 显示Toast
	 * 
	 * @param str
	 * 
	 */
	public void DisplayToast(String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
	
	/* *
	 * 创建对话框
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		ProgressDialog dialog = new ProgressDialog(this);
		// dialog.setTitle("对话框");
		dialog.setMessage("数据加载中...");
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		return dialog;
	}
	
	/**
	 * 关闭对话框并finish掉当前activity
	 * 
	 * @param title
	 * @param msg
	 */
	public void operDialogAndFinish(String title, String msg) {

		Dialog dialog = new AlertDialog.Builder(this).setTitle(title)
				.setMessage(msg)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}

				}).create();
		dialog.show();
	}
	
	/**
	 * 只关闭对话框
	 * 
	 * @param title
	 * @param msg
	 */
	public void operDialogNoFinish(String title, String msg) {

		Dialog dialog = new AlertDialog.Builder(this).setTitle(title)
				.setMessage(msg).setPositiveButton("确定", null).create();
		dialog.show();
	}
	
	/**
	 * 判断是否有网络连接 如无连接，则退出
	 * 
	 * @param context
	 * @return
	 */
	public boolean isNetworkConnected(Context context, boolean isShowDialog) {
		boolean isConnected = false;
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				isConnected = mNetworkInfo.isAvailable();
			}
			if (!isConnected && isShowDialog) {
				new AlertDialog.Builder(context)
						.setTitle("快乐酒店友情提示")
						.setMessage("亲，您的网络不给力，请看看网络是否已经启用。")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int whichButton) {
										setResult(RESULT_OK);// 确定按钮事件
										Intent exit = new Intent(
												Intent.ACTION_MAIN);
										exit.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
										exit.addCategory(Intent.CATEGORY_HOME);
										startActivity(exit);
									}
								}).show();

			}
		}
		return isConnected;
	}

}
